package com.example.spring.cors;

import com.example.spring.spring.importx.ImportOrange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: orange
 * @create: 2023-08-03 00:19
 * @description:
 */
@Configuration
@Import(ImportOrange.class)
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration htmlGet = new CorsConfiguration();
        htmlGet.addAllowedOrigin("null");
        htmlGet.addAllowedMethod("GET");
        htmlGet.addAllowedHeader("Token");

        CorsConfiguration htmlDelete = new CorsConfiguration();
        htmlDelete.addAllowedOrigin("null");
        htmlDelete.setAllowCredentials(false);
        htmlDelete.addAllowedMethod("GET");
        htmlDelete.addAllowedMethod("DELETE");

        UrlBasedCorsConfigurationSource corsConfigurationSource
                = new UrlBasedCorsConfigurationSource();

        corsConfigurationSource.registerCorsConfiguration("/cors/html", htmlGet);
        corsConfigurationSource.registerCorsConfiguration("/cors/delete", htmlDelete);

        return new CorsFilter(corsConfigurationSource);
    }
}
